package com.zhl.media.commonres.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import com.zhl.media.commonres.R;
import com.zhl.media.commonres.view.DialogBottomAdapter;

import java.util.List;

public class BottomDialog extends Dialog {

    private Context mContext;
    private List<String> item;
    private ListView lv_item;
    private TextView tv_cancle,tvTitle,tvDes;
    private DialogBottomAdapter itemDailog;
    private String title = "";
    private String des = "";
    public BottomDialog(@NonNull Context context, List<String> item,String title,String des) {
        super(context);
        this.mContext = context;
        this.item = item;
        this.title = title;
        this.des = des;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window window = getWindow();
        window.setGravity(Gravity.BOTTOM);
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        window.setWindowAnimations(R.style.BottomDialogStyle);//设置从屏幕下方弹框动画
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        View view = LayoutInflater.from(mContext).inflate(R.layout.layout_dialog_bottom, null);
        tvTitle = (TextView) view.findViewById(R.id.tvTitle);
        tvDes = (TextView) view.findViewById(R.id.tvDes);
        lv_item = (ListView) view.findViewById(R.id.lv_item);
        tv_cancle = (TextView) view.findViewById(R.id.tvCancle);
        setContentView(view);
        tv_cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        itemDailog = new DialogBottomAdapter(mContext, R.layout.dialog_item_bottom, item);
        lv_item.setAdapter(itemDailog);
        lv_item.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (itemClickListener != null) {
                    dismiss();
                    itemClickListener.itemClick(position, item.get(position));
                }
            }
        });
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        getWindow().setAttributes(lp);
        setCanceledOnTouchOutside(true);// 点击Dialog外部消失

        if(title.equals("")){
            tvTitle.setVisibility(View.GONE);
        }else {
            tvTitle.setText(title);
        }
        if(des.equals("")){
            tvDes.setVisibility(View.GONE);
        }else {
            tvDes.setText(des);
        }

    }

    private ItemClickListener itemClickListener;

    public void setItemClickListener(ItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }


    public interface ItemClickListener {
        void itemClick(int position, String item);
    }
}
